import vue from '@vitejs/plugin-vue'
import {defineConfig} from 'vite'
// 下面3个是按需要引入ui组件，会自动生成auto-imports.d.ts和components.d.ts文件
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'


import {resolve} from 'path'
// @ts-ignore
import autoprefixer from 'autoprefixer'
import postcssFlexbugsFixes from 'postcss-flexbugs-fixes'
import postcssPresetEnv from "postcss-preset-env"
import removeConsole from "vite-plugin-remove-console";
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// gzip
import viteCompression from 'vite-plugin-compression'

export default defineConfig({
    // base:"/some/",//所有的东西都放在http://127.0.0.1:5173/some/这个文件夹里。base指的是打包后的主应用文件放到服务器的哪个文件夹里和nginx的文件结构有关
    base:"/",//默认将主应用的打包文件放到部署服务器的根文件夹中
    build:{
        chunkSizeWarningLimit:2000,
        target:"es2015",
        outDir:"dist",
        assetsDir:"assets",
        assetsInlineLimit:4096,//导入的资源如果小于4kb就生成base64位，减少请求
        sourcemap:true,

        rollupOptions:{
            input:{
                main:resolve(__dirname,'index.html')
            },
            output:{
                dir:"dist",
                format:"esm",
                entryFileNames:"js/[name]-[hash].js",
                manualChunks(id) {
                    if (id.includes('node_modules')) {
                        return id.toString().split('node_modules/')[1].split('/')[0].toString();
                    }

                }

            }
        },

    },

    css:{
        postcss:{
            plugins:[
                postcssPresetEnv(),//预设
                postcssFlexbugsFixes(),//解决flex的bug
                autoprefixer({
                    overrideBrowserslist:[
                        "> 1%",
                        "last 2 versions"
                    ]
                }),
                {
                    postcssPlugin:"internal:charset-removal",
                    AtRule:{
                        charset:(atRule)=>{
                            if(atRule.name === "charset"){
                                atRule.remove()
                            }
                        }
                    }
                }
            ]

        },
        preprocessorOptions:{
            scss:{
                additionalData:`@import "@/common.scss";`
            }
        }
    },
    resolve:{
        alias:[
            {
                find:"@",
                replacement:resolve(__dirname,"./src")
            },
            {
                find:"~",
                replacement:resolve(__dirname,"./")
            }
        ]
    },
    plugins:[
        viteCompression({
            verbose:true,
            disable:true,
            threshold:1,
            algorithm:"gzip",
            ext:".gz"
        }),
        createSvgIconsPlugin({
            iconDirs:[resolve(process.cwd(),'src/assets/svg')],
            symbolId: 'icon-[dir]-[name]',
        }),
        vue(),
        removeConsole(),
        AutoImport({
            resolvers: [ElementPlusResolver()],
        }),
        Components({
            resolvers: [ElementPlusResolver()],
        })
    ],
    optimizeDeps:{
        entries:[],//指定入口文件
        exclude:[],//排除
        include:[]
    }

})
